#    Rubatom
#    Copyright (c) 2014 Sebastian Dufner
#
#    This file is part of Rubatom.
#
#    Rubatom is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Rubatom is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'rubatom/template/block'
require 'rubatom/template/link'

module Rubatom
  class Template
    class Header
      def self.deserialize(input)
        new(*input.read_many(1, 4, 4, 4, 4, 4, 4))
      end

      def initialize(version, u1, u2, u3, u4, u5, u6)
        @version, @u1, @u2, @u3, @u4, @u5, @u6 = version, u1, u2, u3, u4, u5, u6
      end
      attr_accessor :version, :u1, :u2, :u3, :u4, :u5, :u6

      def serialize(output)
        output.write_many([version, u1, u2, u3, u4, u5, u6] =>
                [1, 4, 4, 4, 4, 4, 4])
      end
    end

    def self.deserialize(input)
      new(Header.deserialize(input), Blocks.deserialize(input),
            Links.deserialize(input))
    end

    def initialize(header, blocks, links)
      @header, @blocks, @links = header, blocks, links
    end
    attr_accessor :header, :blocks, :links

    def serialize(output)
      header.serialize(output)
      blocks.serialize(output)
      links.serialize(output)
    end
  end
end
